package day07;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * <a href="https://leetcode-cn.com/problems/palindrome-number/">9.回文数</a>
 */
public class Answer {

    public static void main(String[] args) {
        // 1234321
        int a = 11;
        // 445
//        System.out.println(isPalindromeNumber(a));

        System.out.println(isPalindromeString(a));

    }

    public static boolean isPalindromeString(int x) {
        StringBuilder sb = new StringBuilder();
        sb.append(x);
        StringBuilder reverse = sb.reverse();
        int a;
        try {
            a = Integer.parseInt(reverse.toString());
        } catch (Exception e) {
            return false;
        }
        return a == x;
    }

    public static boolean isPalindromeNumber(int x) {
        int a = Math.abs(x);
        int count = getCount(a);
        int b;
        if (count % 2 == 0) {
            b = count / 2;
        } else {
            b = (count - 1) / 2;
        }
        int lastNumber = getLastNumber(a, b);
        int firstNumber = getFirstNumber(a, ++b);

        int total = lastNumber + firstNumber;
        int l = getLastNumber(total, 1);

        // 445 / 5 = 89
        // 444 / 4 = 111
        return total % l == 0;
    }

    public static int getCount(int a) {
        int c = 1;
        while ((a / 10) > 0) {
            a = a / 10;
            ++c;
        }
        return c;
    }

    public static int getLastNumber(int a, int count) {
        return (int) (a % Math.pow(10, count));
    }

    public static int getFirstNumber(int a, int count) {
        return (int) (a / Math.pow(10, count));
    }
}
